<template>
  <div class="insurance-page">
    <h2>保险管理</h2>
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon policies">
            <el-icon><Shield /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalPolicies }}</div>
            <div class="stat-label">保险单数</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon premium">
            <el-icon><Money /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.totalPremium) }}</div>
            <div class="stat-label">总保费</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon avg">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.avgPremium) }}</div>
            <div class="stat-label">平均保费</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon rate">
            <el-icon><PieChart /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.insuranceRate }}%</div>
            <div class="stat-label">投保率</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 搜索筛选 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="订单号">
          <el-input v-model="searchForm.order_id" placeholder="请输入订单号" clearable />
        </el-form-item>
        <el-form-item label="保险名称">
          <el-input v-model="searchForm.insurance_name" placeholder="请输入保险名称" clearable />
        </el-form-item>
        <el-form-item label="投保时间">
          <el-date-picker
            v-model="searchForm.order_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 保险数据表格 -->
    <el-card class="table-card">
      <el-table :data="insuranceList" style="width: 100%" v-loading="loading">
        <el-table-column prop="insurance_policy_no" label="投保单号" width="180" />
        <el-table-column prop="order_id" label="订单编号" width="180" />
        <el-table-column prop="order_time" label="下单时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.order_time) }}
          </template>
        </el-table-column>
        <el-table-column prop="transaction_time" label="动账时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.transaction_time) }}
          </template>
        </el-table-column>
        <el-table-column prop="insurance_name" label="保险名称" width="150" />
        <el-table-column prop="insurance_premium" label="支付保费" width="120">
          <template #default="{ row }">
            {{ formatMoney(row.insurance_premium) }}
          </template>
        </el-table-column>
        <el-table-column prop="transaction_serial_no" label="动账流水号" width="200" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

// 搜索表单
const searchForm = reactive({
  order_id: '',
  insurance_name: '',
  order_time: []
})

// 分页信息
const pagination = reactive({
  current: 1,
  size: 20,
  total: 0
})

// 加载状态
const loading = ref(false)

// 统计数据
const stats = ref({
  totalPolicies: 567,
  totalPremium: 12345.67,
  avgPremium: 21.77,
  insuranceRate: 45.8
})

// 保险列表
const insuranceList = ref([
  {
    insurance_policy_no: 'BX202401010001',
    order_id: 'DY202401010001',
    order_time: '2024-01-01 10:00:00',
    transaction_time: '2024-01-01 10:05:00',
    insurance_name: '运费险',
    insurance_premium: 2.50,
    transaction_serial_no: 'TX202401010001',
    remark: '正常投保'
  },
  {
    insurance_policy_no: 'BX202401010002',
    order_id: 'DY202401010002',
    order_time: '2024-01-01 11:00:00',
    transaction_time: '2024-01-01 11:05:00',
    insurance_name: '运费险',
    insurance_premium: 3.00,
    transaction_serial_no: 'TX202401010002',
    remark: '正常投保'
  },
  {
    insurance_policy_no: 'BX202401010003',
    order_id: 'DY202401010003',
    order_time: '2024-01-01 12:00:00',
    transaction_time: '2024-01-01 12:05:00',
    insurance_name: '运费险',
    insurance_premium: 2.80,
    transaction_serial_no: 'TX202401010003',
    remark: '正常投保'
  }
])

// 搜索
const handleSearch = () => {
  pagination.current = 1
  fetchInsuranceList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    order_id: '',
    insurance_name: '',
    order_time: []
  })
  handleSearch()
}

// 查看详情
const handleView = (row: any) => {
  console.log('查看保险详情:', row)
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.size = size
  fetchInsuranceList()
}

// 当前页改变
const handleCurrentChange = (current: number) => {
  pagination.current = current
  fetchInsuranceList()
}

// 获取保险列表
const fetchInsuranceList = async () => {
  try {
    loading.value = true
    // TODO: 调用API获取保险列表
    pagination.total = 100
  } catch (error) {
    ElMessage.error('获取保险列表失败')
  } finally {
    loading.value = false
  }
}

// 格式化金额
const formatMoney = (amount: number) => {
  return `¥${amount.toFixed(2)}`
}

// 格式化日期
const formatDate = (date: string) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

onMounted(() => {
  fetchInsuranceList()
})
</script>

<style scoped>
.insurance-page {
  padding: 20px;
}

.insurance-page h2 {
  margin-bottom: 20px;
  color: #333;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  color: white;
}

.stat-icon.policies {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.premium {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.avg {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.rate {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style> 